﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using ERPStore.Extensions;


namespace ERPStore.QuoteCart.Repositories
{
	public static class ModelConvertor
	{
		public static Models.QuoteCartItem ToCartItemModel(this Datas.QuoteCartItem itemData, ERPStore.Models.Product product)
		{
			var cartItem = new Models.QuoteCartItem();
			cartItem.Id = itemData.Id;
			cartItem.Packaging = product.Packaging.Value;
			cartItem.Product = product;
			cartItem.Quantity = itemData.Quantity;
			cartItem.CreationDate = itemData.CreationDate;
			cartItem.LastUpdate = itemData.LastUpdate;
			cartItem.AddFrom = itemData.AddFrom;
			cartItem.OfferName = itemData.OfferName;
			cartItem.InventoryTimeStateId = itemData.InventoryTimeStateId;
			return cartItem;
		}

		public static Datas.QuoteCartItem ToCartItemData(this Models.QuoteCartItem model)
		{
			var data = new Datas.QuoteCartItem();
			data.Id = model.Id;
			data.ProductId = model.Product.Id;
			data.Quantity = model.Quantity;
			data.CreationDate = model.CreationDate;
			data.LastUpdate = model.LastUpdate;
			data.AddFrom = model.AddFrom;
			data.OfferName = model.OfferName;
			data.InventoryTimeStateId = model.InventoryTimeStateId;

			return data;
		}


		public static Models.QuoteCart ToQuoteCartModel(this Datas.QuoteCart cartData)
		{
			if (cartData == null)
			{
				return null;
			}
			var catalogService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ICatalogService>();
			var cart = new Models.QuoteCart();
			cart.Id = cartData.Id;
			cart.Code = cartData.Code;
			cart.ConversionDate = cartData.ConversionDate;
			cart.ConvertedEntityId = cartData.ConvertedEntityId;
			cart.CreationDate = cartData.CreationDate;
			cart.CustomerDocumentReference = cartData.CustomerDocumentReference;
			cart.Message = cartData.Message;
			cart.VisitorId = cartData.VisitorId;
			cart.CustomerId = cartData.CustomerPersonId;
			cart.LastPage = cartData.LastPage;
			cart.LeadSourceInfo = cartData.LeadSourceInfo;
			cart.CartStateId = cartData.CartStateId;

			foreach (var itemData in cartData.Items)
			{
				var product = catalogService.GetProductById(itemData.ProductId);
				if (product == null)
				{
					continue;
				}
				if (itemData.IsArchived)
				{
					continue;
				}
				var item = itemData.ToCartItemModel(product);
				if (item != null)
				{
					cart.Items.Add(item);
				}
			}

			return cart;
		}

		public static Datas.QuoteCart ToQuoteCartData(this Models.QuoteCart model, ERPStore.Models.UserPrincipal principal)
		{
			if (model == null)
			{
				return null;
			}
			var data = new Datas.QuoteCart();
			data.Id = model.Id;
			data.Code = model.Code;
			data.ConversionDate = model.ConversionDate;
			data.ConvertedEntityId = model.ConvertedEntityId;
			data.CreationDate = model.CreationDate;
			data.CustomerDocumentReference = model.CustomerDocumentReference;
			data.Message = model.Message;
			data.VisitorId = model.VisitorId;
			if (principal.CurrentUser != null)
			{
				data.CustomerPersonId = principal.CurrentUser.Id;
			}
			data.LastPage = model.LastPage;
			data.LeadSourceInfo = model.LeadSourceInfo;
			data.CartStateId = model.CartStateId;

			data.Items = new List<Datas.QuoteCartItem>();
			foreach (var itemModel in model.Items)
			{
				var item = itemModel.ToCartItemData();
				if (item != null)
				{
					data.Items.Add(item);
					item.QuoteCartId = model.Id;
				}
			}

			return data;
		}

	}
}
